//MaHuTipLayer.js
(function (exports) {

    var $ = null;

    var MaHuTipLayer = cc.Layer.extend({
        ctor: function (hupaiCards) {
            this._super();

            var that = this;

            var scene = ccs.load(res.MaHuTipLayer_json);
            that.addChild(scene.node);

            $ = create$(this.getChildByName("Node"));

            this.initHuPaiTishiLayer(hupaiCards);
        },
        initHuPaiTishiLayer: function (hupaiCards) {
            var self = this;
            var hupai_tip = self;
            if(!hupaiCards)  return;
            hupai_tip.setLocalZOrder(10);
            hupai_tip.setVisible(true);
            $('hupai_tip.bg').setVisible(false);
            var tipsListener = cc.EventListener.create({
                event: cc.EventListener.TOUCH_ONE_BY_ONE,
                swallowTouches: true,
                onTouchBegan: function (touch, event) {
                    if (!TouchUtil.isTouchMe($('hupai_tip'), touch, event)) {
                        return false;
                    }
                    $('hupai_tip.bg').setVisible(true);
                    return true;
                },
                onTouchMoved: function (touch, event) {
                },
                onTouchEnded: function (touch, event) {
                    $('hupai_tip.bg').setVisible(false);
                },
                onTouchCancelled: function (touch, event) {
                    $('hupai_tip.bg').setVisible(false);
                }
            });
            cc.eventManager.addListener(tipsListener, $('hupai_tip'));

            // TouchUtil.setOnclickListener($('hupai_tip'), function () {
            //     $('hupai_tip.bg').setVisible(!$('hupai_tip.bg').isVisible());
            // }, {effect: TouchUtil.effects.NONE});
            for (var i = 0; i < 28; i++) {
                if ($('hupai_tip.bg').getChildByName('bg' + i)) {
                    $('hupai_tip.bg').getChildByName('bg' + i).setVisible(false);
                }
            }
            // hupaiCards = [[1,1,1], [1,1,1], [1,1,1], [1,1,1], [1,1,1], [1,1,1], [1,1,1], [1,1,1],[1,1,1], [1,1,1], [1,1,1], [1,1,1], [1,1,1], [1,1,1], [1,1,1], [1,1,1],[1,1,1], [1,1,1], [1,1,1], [1,1,1], [1,1,1], [1,1,1], [1,1,1], [1,1,1],[1,1,1]];
            var rowCount = 6;
            var hulength = hupaiCards.length;
            var rowNum = Math.floor((hulength + rowCount - 1) / rowCount);
            var isHuAny = false;
            if (hulength >= 27) {
                isHuAny = true;
                rowNum = 1;
                hulength = 1;
            }
            var txt = $('hupai_tip.bg').getChildByName('huAnyTxt');
            if (txt) txt.setVisible(false);
            $('hupai_tip.bg').setContentSize(cc.size(((hulength >= rowCount) ? rowCount : hulength) * 150 + 150, rowNum * 110 + 40));
            if (isHuAny) {
                if (!txt) {
                    txt = new ccui.Text();
                    txt.setName('huAnyTxt');
                    txt.setString('胡任意牌');
                    txt.setFontSize(35);
                    txt.setTextColor(cc.color(0, 255, 0));
                    txt.enableOutline(cc.color(38, 38, 38), 1);
                    txt.setPosition(cc.p(180, $('hupai_tip.bg').getContentSize().height / 2));
                    $('hupai_tip.bg').addChild(txt);
                }
                txt.setVisible(true);
                return;
            }
            for (var i = 0; i < hulength; i++) {
                (function (rowi) {
                    var newlie = rowi % rowCount;
                    var newhang = rowNum - Math.floor(i / rowCount);
                    var bg0 = $('hupai_tip.bg.bg0');
                    var bg = $('hupai_tip.bg').getChildByName('bg' + rowi);
                    if (!bg) {
                        bg = duplicateNode(bg0, true);
                        bg.setName('bg' + rowi);
                        bg0.getParent().addChild(bg);
                    }
                    bg.setPosition(cc.p(bg0.getPositionX() + 150 * (newlie), (newhang - 0.5) * 110 + 20));
                    bg.setVisible(true);
                    var paiName = MaLayer.getPaiNameByRowAndId(2, hupaiCards[rowi][0], true, true);
                    var card = bg.getChildByName('pai');
                    var bei_num = bg.getChildByName('bei_num');
                    var zhang_num = bg.getChildByName('zhang_num');
                    if (!card) {
                        card = new cc.Sprite();
                        card.setName('pai');
                        card.setPosition(cc.p(45, 50));
                        bg.addChild(card);
                    }
                    setSpriteFrameByName(card, paiName, 'pai');
                    if (bei_num) bei_num.setString(hupaiCards[rowi][1]);
                    if (bei_num) zhang_num.setString(hupaiCards[rowi][2]);
                })(i);
            }
        },
    });

    exports.MaHuTipLayer = MaHuTipLayer;
})(this);
